package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.AboutInfoMapper;
import com.jd.finance.core.mapper.NoticeMapper;
import com.jd.finance.core.pojo.entity.AboutInfo;
import com.jd.finance.core.pojo.entity.Notice;
import com.jd.finance.core.service.NoticeService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-19
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private AboutInfoMapper aboutInfoMapper;

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;

    @Autowired
    private OpreateLogService opreateLogService;

    /**
     * 查询最新一条公告
     *
     * @return
     */
    @Override
    public BaseResult getNewAnnounceTop() {
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        qw.select("notice_text")
                .orderByDesc("create_time")
                .last("limit 1");
        Notice notice = noticeMapper.selectOne(qw);
        if (notice == null) {
            return BaseResult.error().message("未查询到最新公告!");
        }
        return BaseResult.ok().data("announcementContent", notice.getNoticeText());
    }

    /**
     * 查询最新的六条公告
     *
     * @return
     */
    @Override
    public BaseResult getNewSixAnnounce() {
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        qw.orderByDesc("create_time")
                .last("limit 6");
        List<Notice> list = noticeMapper.selectList(qw);
        if (list == null && list.size() == 0) {
            return BaseResult.error().message("未查询到最新六条公告!");
        }

        return BaseResult.ok().data("noticeList", list);
    }

    /**
     * 查询关于我们
     * @return
     */
    @Override
    public BaseResult getAboutWe() {
        QueryWrapper<AboutInfo> qw = new QueryWrapper<>();
        qw.select("about_us");
        AboutInfo aboutInfo = aboutInfoMapper.selectOne(qw);
        if (aboutInfo == null){
            return BaseResult.error().message("关于我们为空!");
        }
        return BaseResult.ok().data("aboutusContent:",aboutInfo.getAboutUs());
    }

    /**
     * 查看所有公告
     * @param uid
     * @param page
     * @param limit
     * @return
     */
    @Override
    public BaseResult getAPPAllNotice(Integer uid,Long page, Long limit) {
        if (uid==null) {
            log.error("id为空!  s");
            return BaseResult.error().message("id为空  s");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)) {
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        //倒序+分页
        QueryWrapper qw = new QueryWrapper<>();
        qw.orderByDesc("create_time");
        Page<Notice> page1 = new Page<>(page,limit);
        Page page2 = noticeMapper.selectPage(page1, qw);

        return BaseResult.ok().data("notice:",page2);
    }

    /**
     * 根据公告id查询公告
     * @param uid
     * @param nid
     * @return
     */
    @Override
    public BaseResult getAPPNoticeById(Integer uid, Integer nid) {
        if (uid==null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)) {
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        Notice notice = noticeMapper.selectById(nid);

        return BaseResult.ok().data("notice:",notice);
    }

    /**
     *根据公告id删除公告
     * @param uid
     * @param nid
     * @param request
     * @return
     */
    @Override
    public BaseResult deleteAPPNoticeById(Integer uid, Integer nid, HttpServletRequest request) {
        if (uid==null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)) {
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        int i = noticeMapper.deleteById(nid);
        if (i==0){
            log.error("删除失败!");
            return BaseResult.error().message("删除失败");
        }
        boolean add=opreateLogService.add(uid,"删除","删除公告", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("shanchujieguo:",i);
    }

    /**
     * 添加新公告
     * @param uid
     * @param title
     * @param text
     * @return
     */
    @Override
    public BaseResult insertAPPNotice(Integer uid, String title, String text,HttpServletRequest request) {
        if (uid==null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }
        if (!adminUtilsServiceimpl.isInformation(uid)) {
            log.error("当前登录用户不是信息管理员账户");
            return BaseResult.error().message("当前登录用户不是信息管理员账户");
        }
        Notice notice = new Notice();
        notice.setNoticeTitle(title);
        notice.setNoticeText(text);
        int i = noticeMapper.insert(notice);
        if (StringUtils.isAllBlank(title)){
            log.error("标题为空!");
            return BaseResult.error().message("标题为空");
        }
        if (StringUtils.isAllBlank(text)){
            log.error("内容为空!");
            return BaseResult.error().message("内容为空");
        }
        if (i==0){
            log.error("添加失败!");
            return BaseResult.error().message("添加失败");
        }
        boolean add=opreateLogService.add(uid,"添加","添加新公告", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("tianjiajieguo:",i);
    }
}
